<?php

/**
 * camara actions.
 *
 * @package    camaras
 * @subpackage camara
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class camaraActions extends sfActions
{

  public function executeShow(sfWebRequest $request)
  {
    $this->camara = Doctrine::getTable('Camara')->find($request->getParameter('cd_camara'));
  }

  public function executePrincipal(sfWebRequest $request)
  {
	  if ($this->getUser()->getAttribute('reset_senha',null,'usr')) {
	  	$this->redirect('usuario/resetSenha');
	  } else {
	    $this->camara_list = ($this->getUser()->getAttribute('gerente',null,'usr')) ?
		  Doctrine::getTable('Camara')
		    ->createQuery('a')
        	->where('deleted = 0')
	        ->orderBy('name')
		    ->execute()
		:
		  Doctrine::getTable('Camara')->getCamarasSecretario($this->getUser()->getAttribute('cd_usuario',null,'usr'));
	  }
  }  
  
  public function executeIndex(sfWebRequest $request)
  {
	    $this->camara_list = ($this->getUser()->getAttribute('gerente',null,'usr')) ?
		  Doctrine::getTable('Camara')
		    ->createQuery('a')
        	->where('deleted = 0')
	        ->orderBy('name')
		:
		  Doctrine::getTable('Camara')->getCamarasSecretario($this->getUser()->getAttribute('cd_usuario',null,'usr'));
		  
		$this->pager = new sfDoctrinePager(
			'Camara',
			sfConfig::get('app_qtd_registros_por_pagina')
		);

		$this->pager->setPage($request->getParameter('page', 1));
		$this->pager->setQuery($this->camara_list);
		$this->pager->init();

  }

  public function executeShowAcoes(sfWebRequest $request)
  {
    $this->acoes = Doctrine::getTable('Acao')->getAcoesPorCamara($request->getParameter('cd_camara'), $cd_usuario = null, $dt_start = null, $dt_end = null, $so_acoes_abertas = true);
	
    $this->pager = new sfDoctrinePager(
		'Acao',
		sfConfig::get('app_qtd_registros_por_pagina')
	);

	$this->pager->setPage($request->getParameter('page', 1));
    $this->pager->setQuery($this->acoes);
	$this->pager->init();
	
    $this->camara = Doctrine::getTable('Camara')->find($request->getParameter('cd_camara'));
	
	$this->getUser()->setAttribute('id', $request->getParameter('cd_camara'), 'camara');
	$this->getUser()->setAttribute('last_page', @$_SERVER['ORIG_PATH_INFO'], 'usr');
	$this->getUser()->setAttribute('show_acoes_page', @$_SERVER['ORIG_PATH_INFO'], 'usr');

  }

  public function executeShowProposicoes(sfWebRequest $request)
  {
    $q= Doctrine::getTable('Proposicao')->getProposicoesPorCamara($request->getParameter('cd_camara'), $so_abertas = 1);
    $this->proposicoes = $q->execute();
    $this->camara = Doctrine::getTable('Camara')->find($request->getParameter('cd_camara'));
	$this->getUser()->setAttribute('show_proposicoes_page', @$_SERVER['ORIG_PATH_INFO'], 'usr');
  }

  public function executeNew(sfWebRequest $request)
  {
	if(!$this->getUser()->getAttribute('gerente',null,'usr')){
	  $this->getUser()->setFlash('error', sfConfig::get('app_msg_cadastro_exclusivo_gerente'));
	  $this->redirect('@homepage');
	} else {
      $this->form = new CamaraForm();
	}
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new CamaraForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($camara = Doctrine::getTable('Camara')->find(array($request->getParameter('cd_camara'))), sprintf('Object camara does not exist (%s).', array($request->getParameter('cd_camara'))));
    $this->form = new CamaraForm($camara);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($camara = Doctrine::getTable('Camara')->find(array($request->getParameter('cd_camara'))), sprintf('Object camara does not exist (%s).', array($request->getParameter('cd_camara'))));
    $this->form = new CamaraForm($camara);

    $this->processForm($request, $this->form);

    $this->setTemplate('index');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($camara = Doctrine::getTable('Camara')->find(array($request->getParameter('cd_camara'))), sprintf('Object camara does not exist (%s).', array($request->getParameter('cd_camara'))));
    $camara->delete();

    $this->getUser()->setFlash('notice', sfConfig::get('app_msg_confirmacao'));			
    $this->redirect('camara/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()));
    if ($form->isValid())
    {
      $camara = $form->save();

      $this->getUser()->setFlash('notice', sfConfig::get('app_msg_confirmacao'));
      $this->redirect('camara/index');
    }
  }
}